package com.acme;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Player {

	public static final String PLAYER_ID = "player_id";
	
	public static enum STATUS {Active, Retired, Unknown}
	public static enum POSITION {
        QB ("QB"), NA ("?"), WR ("WR"), RB("RB"), FB("FB"), TE("TE"), OL("OL"), DL("DL"), LB("LB"), DB("DB"), K("K"), P("P"), ST("ST");
        private String value;

        POSITION(String value){
           this.value = value;
        }

        public String toString(){
            return value;
        }

        public static POSITION getByValue(String value){
            POSITION returnValue = null;
            for (final POSITION element : EnumSet.allOf(POSITION.class)) {
                if (element.toString().equals(value)) {
                    returnValue = element;
                }
            }
            return returnValue;
        }
	}
	
	public String[] positions;
	public String id;
	public String firstName;
	public String hometown;
	public String weight;
	public String height;
	public String nickname;
	public String dob;
	public String college;
	public String lastName;
	public String[] teams;
	public String drafted;
	public ArrayList<Accolade> awards;
	public HashMap<String,String> yearsByAward;
	
	private String name;
	private Player.STATUS status;
	//private Player.POSITION position;
	private String school;

	public static Player getPlayer(String JSONplayer){
		try {
			return new Player(new JSONObject(JSONplayer));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ArrayList<Player> getPlayers(String JSONplayers){
		try {
			return getPlayers(new JSONArray(JSONplayers));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ArrayList<Player> getPlayers(JSONArray players){
		
		ArrayList<Player> playerList = new ArrayList<Player>();
		
		try {
			int length = players.length();
			for (int i = 0; i < length; i++){
				JSONObject player = players.getJSONObject(i);
				playerList.add(new Player(player));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return playerList;
	}
	
	public Player(){
		positions = new String[1];
		positions[0] = "";
		id = "";
		firstName = "";
		hometown = "";
		weight = "";
		height = "";
		nickname = "";
		dob = "";
		college = "";
		lastName = "";
		teams = new String[1];
		teams[0] = "";
		drafted = "";
	}

	public void collapseAwardsByName(){
		Iterator<Accolade> it = awards.iterator();
		while (it.hasNext()){
			Accolade award = it.next();
			if (yearsByAward.get(award.name) != null){
				String awards = yearsByAward.get(award.name);
				yearsByAward.put(award.name,awards+","+award.year); 	
			} else {
				yearsByAward.put(award.name,award.year);
			}
			
		}
	}
	
	public Player(JSONObject player){
		
		String nullStr = "null";
		
		try {
			
			positions = this.parsePositions(player.getString("position"));
			if (positions.equals(nullStr)){
				positions = new String[1];
				positions[0] = "";
			}
			
			id = player.getString("id");
			if (id.equals(nullStr)){
				id = "";
			}
			
			firstName = player.getString("first_name");
			if (firstName.equals(nullStr)){
				firstName = "";
			}
			
			hometown = player.getString("hometown");
			if (hometown.equals(nullStr)){
				hometown = "";
			}
			
			weight = player.getString("weight");
			if (weight.equals(nullStr)){
				weight = "";
			}
			
			height = player.getString("height");
			if (height.equals(nullStr)){
				height = "";
			}
			
			nickname = player.getString("nickname");
			if (nickname.equals(nullStr)){
				nickname = "";
			}
			
			dob = player.getString("dob");
			if (dob.equals(nullStr)){
				dob = "";
			}
			
			college = player.getString("college");
			if (college.equals(nullStr)){
				college = "";
			}
			
			lastName = player.getString("last_name");
			if (lastName.equals(nullStr)){
				lastName = "";
			}
			
			teams = this.parseTeams(player.getString("team"));
			if (teams.equals(nullStr)){
				teams = new String[1];
				teams[0] = "";
			}
			drafted = player.getString("drafted");
			if (drafted.equals(nullStr)){
				drafted = "";
			}

			JSONArray awardsJson = player.optJSONArray("awards");
			if (awardsJson == null){
				awards = new ArrayList<Accolade>();
				yearsByAward = new HashMap<String,String>();
			} else {
				awards = Accolade.getAccolades(awardsJson);
				yearsByAward = new HashMap<String,String>();
				collapseAwardsByName();
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public String getAllTeams(){
		String allteams = teams[0];
    	for (int x=1;x < teams.length-1; x++){
    		allteams += ","+teams[x];
    	}
    	return allteams;
	}
	
	private String[] parsePositions(String allPositions){
		return allPositions.split("/");
	}
	
	private String[] parseTeams(String allTeams){
		return allTeams.split(",");
	}
	
	public String toString(){
		String player = "Name: "+firstName+" "+lastName;
		return player;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Player.STATUS getStatus() {
		return status;
	}
	
	public String getStatusString(){
		if(status == STATUS.Active)
			return "A";
		else if (status == STATUS.Retired)
			return "R";
		else 
			return "?";
	}

	public void setStatus(Player.STATUS status) {
		this.status = status;
	}

	public String getSchool() {
		return school;
	}

	public void setSchool(String school) {
		this.school = school;
	}
	
}
